<?php 
    require_once $_SERVER['DOCUMENT_ROOT'] . '/accHestia/src/svr/com/acc_flock.php';
    require_once $_SERVER['DOCUMENT_ROOT'] . '/accHestia/src/svr/com/rpc_com.php';

    $svr_path = $_SERVER['DOCUMENT_ROOT'] . "/accHestia/src/svr/";
    $rpcs = array(
        "req_login"             => array("inc" => $svr_path . "login/login_s.php", "func" => "dorpc_login"),
        "logout"                => array("inc" => $svr_path . "login/login_s.php", "func" => "dorpc_logout"),
        "regist_new"            => array("inc" => $svr_path . "login/login_s.php", "func" => "dorpc_regist_new"),
        "uid_unique_check"      => array("inc" => $svr_path . "login/login_s.php", "func" => "dorpc_uid_unique_check"),
        "confirm_mysql_conn"    => array("inc" => $svr_path . "init/index_s.php", "func" => "dorpc_check_mysql_conn"),
        "rebuild_acc_db"        => array("inc" => $svr_path . "init/index_s.php", "func" => "dorpc_rebuild_acc_db"),
        "set_admin_pwd"         => array("inc" => $svr_path . "init/index_s.php", "func" => "dorpc_set_admin_pwd"),
        "set_acc_category"      => array("inc" => $svr_path . "init/index_s.php", "func" => "dorpc_set_acc_category"),
        "test_autogen_user"     => array("inc" => $svr_path . "init/test_v/genUsers_s.php", "func" => "dorpc_gen_users"),
        "test_add_gened_users"  => array("inc" => $svr_path . "init/test_v/genUsers_s.php", "func" => "dorpc_add_gened_users"),
        "test_autogen_company"  => array("inc" => $svr_path . "init/test_v/genCompany_s.php", "func" => "dorpc_autogen_company"),
        "test_add_gened_compay" => array("inc" => $svr_path . "init/test_v/genCompany_s.php", "func" => "dorpc_add_gened_compay"),
        "test_autogen_prj"      => array("inc" => $svr_path . "init/test_v/genPrj_s.php", "func" => "dorpc_autogen_prj"),
        "test_add_gened_prj"    => array("inc" => $svr_path . "init/test_v/genPrj_s.php", "func" => "dorpc_add_gened_prj"),

        "vouch_save"            => array("inc" => $svr_path . "vochManage/newVoch_s.php", "func" => "dorpc_acc_voch_save"),
        "vochzi_changed_req"    => array("inc" => $svr_path . "vochManage/newVoch_s.php", "func" => "dorpc_voch_new"),
        "pznew_a"               => array("inc" => $svr_path . "vochManage/newVoch_s.php", "func" => "dorpc_voch_new"),
        "voch_query_req"        => array("inc" => $svr_path . "vochManage/vochQuery_s.php", "func" => "dorpc_qvochbase"), 

        "accSet_save"           => array("inc" => $svr_path . "accSetting/accSet_s.php", "func" => "dorpc_accSet_save"),

        "load_acc_data"         => array("inc" => $svr_path . "com/rpc_load_data.php", "func" => "dorpc_load_acc_data"), 
    );

    try {
//        set_error_handler('acc_err_handler');
        $pdata = $_POST["req_param"];
        if(!isset($pdata)) {
            set_res_NG($res, "req_param error", "request Failed.");
            echo json_encode($res);
            return;
        }

        $res = array();
        $method_name = $pdata["rpc_name"];
        if(!isset($method_name)) {
            set_res_NG($res, "rpc method name error", "request Failed.");
            echo json_encode($res);
            return;
        }
        $res["rpc_req_name"] = $method_name;

        $action_func_name = $rpcs[$method_name]["func"];
        if(!isset($action_func_name)) {
            set_res_NG($res, "no service for rpc method:" . $method_name, "request Failed.");
            echo json_encode($res);
            return;
        }
        $res["rpc_acttion_name"] = $action_func_name;

        if(!file_exists($rpcs[$method_name]["inc"])) {
            set_res_NG($res, "not sub inlcude file:" . $rpcs[$method_name]["inc"], "server internal error.");
            echo json_encode($res);
            return;
        }

        include $rpcs[$method_name]["inc"];

        $hdl_file = acc_lock("lock_key_acc_db");
        call_user_func_array($action_func_name, array($pdata, &$res));
        acc_unlock($hdl_file);
        echo json_encode($res);
    } catch (Exception $e) {
        acc_unlock($hdl_file);
        set_res_NG($res, $e->getMessage(), "request Failed.");
        echo json_encode($res);
    }

?>
